package com.cskaoyan.service;

import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.MarketIssue;
import com.cskaoyan.bean.po.MarketIssueExample;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @program: SSMItem
 * @description 该service为通用问题功能的数据处理
 * @author: YY
 * @create: 2022-10-11 18:53
 **/
@Service
public class AdminIssueServiceImpl implements AdminIssueService {

    @Autowired
    MarketIssueMapper mapper;


    //通用问题
    @Override
    public List<MarketIssue> selectIssue(BaseParam baseParam, String question) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketIssueExample marketIssueExample = new MarketIssueExample();

        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        marketIssueExample.setDistinct(true);

        marketIssueExample.setOrderByClause(baseParam.getSort()+" "+baseParam.getOrder());

        //判断是搜索还是查询全部
        //question.trim().length() == 0  判断输入的是否为空串，如果是空串则查询全部信息
        if (!StringUtil.isEmpty(question) && !(question.trim().length() == 0)) {

            //注意在这里需要拼接"%"
            criteria.andQuestionLike("%" + question.trim() + "%");
            List<MarketIssue> issueList = mapper.selectByExample(marketIssueExample);
            return issueList;
        }
        //marketIssueExample为空查询全部记录
        List<MarketIssue> marketIssue = mapper.selectByExample(marketIssueExample);


        return marketIssue;

    }


    @Override
    public MarketIssue insertIssue(MarketIssue marketIssue) {
        //判断是否逻辑删除
        Integer deleted = mapper.selectDeletedByIssue(marketIssue.getQuestion());


        if (!StringUtils.isEmpty(deleted)) {

            Integer id = mapper.selectIdByQuestion(marketIssue.getQuestion());
            marketIssue.setUpdateTime(new Date());
            marketIssue.setAddTime(new Date());
            marketIssue.setId(id);
            marketIssue.setDeleted(false);
            //说明存在 但是逻辑删除了
            int i = mapper.updateByPrimaryKey(marketIssue);
            //查询刚刚插入的数据
            MarketIssue Issue = mapper.selectByPrimaryKey(marketIssue.getId());
            return Issue;

        }


        marketIssue.setAddTime(new Date());

        marketIssue.setUpdateTime(new Date());
        marketIssue.setDeleted(false);

        //插入
        mapper.insert(marketIssue);

        //查询刚刚插入的数据
        MarketIssue Issue = mapper.selectByPrimaryKey(marketIssue.getId());
        return Issue;
    }

    @Override
    public MarketIssue updateIssue(MarketIssue marketIssue) {

        marketIssue.setUpdateTime(new Date());
        mapper.updateByPrimaryKey(marketIssue);

        //查询刚刚修改的数据
        MarketIssue Issue = mapper.selectByPrimaryKey(marketIssue.getId());
        return Issue;
    }

    @Override
    public void deleteIssue(MarketIssue marketIssue) {
        //逻辑删除
        mapper.updateIssueById(marketIssue.getId());
    }

    @Override
    public String selectIssueByQuestion(String question) {
        String question1 = mapper.selectIssue(question);
        return question1;
    }
}
